<template>
  <div class="register">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
      <el-form-item label="" prop="loginCode"><el-input type="text" autocomplete="off" v-model="ruleForm.loginCode"
          prefix-icon="el-icon-user-solid" placeholder="请输入用户名"></el-input></el-form-item>
      <el-form-item label="" prop="userName"><el-input type="text" autocomplete="off" v-model="ruleForm.userName"
          prefix-icon="el-icon-user-solid" placeholder="请输入用户昵称"></el-input></el-form-item>
      <el-form-item label="" prop="password"><el-input type="password" autocomplete="off" v-model="ruleForm.password"
          prefix-icon="el-icon-lock" placeholder="请输入密码"></el-input></el-form-item>
      <el-form-item label="" prop="checkPass"><el-input type="password" autocomplete="off" v-model="ruleForm.checkPass"
          prefix-icon="el-icon-lock" placeholder="确认密码"></el-input></el-form-item>
      <el-form-item label="" prop="mgrType">
        <el-select style="width: 100%;" v-model="ruleForm.mgrType" placeholder="请选择账户类型">
          <el-option label="普通用户" value="0"></el-option>
          <el-option label="系统管理员" value="1"></el-option>
          <el-option label="二级管理员" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="btns">
        <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }

    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    return {
      activeName: 'second',
      ruleForm: {
        loginCode: '',
        password: '',
        checkPass: ''
      },
      rules: {
        loginCode: [
          { required: true, message: '请输入您的名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入您的昵称', trigger: 'blur' }
        ],
        password: [{ required: true, validator: validatePass, trigger: 'blur' }],
        checkPass: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ],
        mgrType: [{ required: true, message: '请选择账户类型', trigger: 'blur' }]
      }
    }
  },

  methods: {
    /**
     * 提交注册信息
     * @param {*} formName
     */
    submitForm (formName) {
      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.$http.common('/app/register', this.ruleForm).then(res => {
            window.console.log('[信息]提交注册信息', res)
            if (res.status === 200 && res.data.result === 'true') {
              this.$message({
                type: 'success',
                message: res.data.message
              })
              _this.activeName = 'first'
              _this.resetForm()
            } else {
              this.$message({
                type: 'error',
                message: '注册异常:' + res.data.message
              })
            }
           })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style scoped lang="less">
.register {
  margin-left: 10%;
  margin-right: 10%;
  margin-top: 8%;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
